<template>
	<div class="addProject business-workAdd">
		<div class="addForm zll-form">
			<el-form :model="addForm" :rules="rules" ref="addForm" class="demo-ruleForm">
				<div v-if="type == 'add'">
					<el-row :gutter="40" class="Search_Top_Input">
						<el-col :span="8">
							<el-form-item class="formList" prop="data1" label="客户：">
								<el-input clearable class="input_right" v-model="queryForm.customer"
										  placeholder="请输入客户/订单号"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item class="formList" prop="data2" label="下单日期：">
								<el-date-picker v-model="queryForm.time"
												type="daterange"
												range-separator="-"
												start-placeholder="开始日期"
												end-placeholder="结束日期">
								</el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<div class="search_bt" v-if="type == 'add'">
								<span class="zll-search" @click="getOrderInfoByParam">搜索</span>
								<span class="zll-search-reset" @click="searchReset()">重置</span>
							</div>
						</el-col>
					</el-row>
					<div class="tableList" v-loading="tableLoading">
						<table border="1">
							<tr>
								<th width="40"></th>
								<th>订单号</th>
								<th>品类</th>
								<th>品名</th>
								<th width="200">规格</th>
								<th>单件(万支)</th>
								<th>件数</th>
								<th>客户</th>
								<th>已发货</th>
							</tr>
							<tr v-if="orderList.length == 0" class="noData">
								<td colspan="8">暂无数据</td>
							</tr>
							<tr v-else class="add_Table" v-for="(item, index) in orderList" :key="index">
								<td>
									<el-checkbox v-model="item.checked" @change="changeBox(item)"></el-checkbox>
								</td>
								<td><p>{{ item.orderNum }}</p></td>
								<td><p>{{ item.itemTypeId }}</p></td>
								<td><p>{{ item.productName }}</p></td>
								<td><p>{{ item.gradeValue }}</p></td>
								<td><p>{{ item.thNumber }}</p></td>
								<td><p>{{ item.peieceNum}}</p></td>
								<td><p>{{ item.customer}}</p></td>
								<td><p>{{ item.storageOutProductCount}}</p></td>

							</tr>
						</table>
					</div>
				</div>
				<div v-else>
					<el-row :gutter="40" style="margin-top: 20px;">
						<el-col :span="8">
							<el-form-item class="formList" prop="data1" label="客户：">
								<el-input clearable class="input_right" v-model="addForm.customer"
										  disabled></el-input>
							</el-form-item>
							<el-form-item class="formList" prop="data2" label="退货号：">
								<el-input clearable class="input_right" v-model="addForm.orderBackCode"
										  disabled></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item class="formList" prop="data3" label="退货状态：">
								<el-input clearable class="input_right" v-model="addForm.orderBackStatusCn"
										  disabled></el-input>
							</el-form-item>
							<el-form-item class="formList" v-if="addForm.disOrderUser ==''"></el-form-item>
							<el-form-item class="formList" v-else prop="data5" label="取消人：">
								<el-input clearable class="input_right" v-model="addForm.disOrderUser+' '+addForm.disOrderTime"
										  disabled></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item class="formList" prop="data5" label="受理人：">
								<el-input clearable class="input_right" v-model="addForm.handleUser+' '+addForm.handleTime"
										  disabled></el-input>
							</el-form-item>


						</el-col>
					</el-row>
					<div class="tableList" style="margin-top: 10px;">
						<table border="1">
							<tr>
								<th>订单号</th>
								<th>品类</th>
								<th>品名</th>
								<th width="200">规格</th>
								<th>单件(万支)</th>
								<th>件数</th>
								<th>已发货</th>
							</tr>
							<tr class="add_Table">
								<td><p>{{ orderInfo.orderNum }}</p></td>
								<td><p>{{ orderInfo.itemTypeId }}</p></td>
								<td><p>{{ orderInfo.productName }}</p></td>
								<td><p>{{ orderInfo.gradeValue }}</p></td>
								<td><p>{{ orderInfo.thNumber }}</p></td>
								<td><p>{{ orderInfo.peieceNum}}</p></td>
								<td><p>{{ orderInfo.storageOutProductCount}}</p></td>
							</tr>
						</table>
					</div>
					<div v-if="addForm.orderBackStatus > '0'">
						<div class="nav_list">
							<div class="_title"><i class="iconfont icon-daifahuo"></i>退货入库</div>
							<el-row :gutter="40">
								<el-col :span="8">
									<el-form-item class="formList" prop="data1" label="物流到件：">
										<el-input clearable class="input_right" v-model="addForm.wuliuCount"
												  disabled></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item class="formList" prop="data3" label="备注：">
										<el-input clearable class="input_right" v-model="addForm.remark"
												  disabled></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item class="formList" prop="data5" label="报数人：">
										<el-input clearable class="input_right" v-model="addForm.baoshuUser+' '+addForm.baoshuTime"
												  disabled></el-input>
									</el-form-item>
								</el-col>
							</el-row>
							<div class="tableList" style="margin-top: 10px;">
								<table border="1" v-show="addForm.orderBackStatus > '2'">
									<tr>
										<th>入库类型</th>
										<th>入库号</th>
										<th>计量单位</th>
										<th>件数</th>
										<th>入库时间</th>
										<th>入库人</th>
										<th>备注</th>
									</tr>
									<tr class="add_Table" v-for="(storage ,si) in storageList" :key="storage.id">
										<td><p>{{ storage.backStorageTypeCn }}</p></td>
										<td><p>{{ storage.backStorageCode }}</p></td>
										<td><p>{{ storage.backUnit }}</p></td>
										<td><p>{{ storage.backCount }}</p></td>
										<td><p>{{ storage.createTimeFt }}</p></td>
										<td><p>{{ storage.backUser }}</p></td>
										<td><p>{{ storage.remark }}</p></td>
									</tr>
								</table>
							</div>
						</div>
						<div class="nav_list">
							<div class="_title"><i class="iconfont icon-daifahuo"></i>退货入库</div>
							<el-row :gutter="40">
								<el-col :span="8">
									<el-form-item class="formList" prop="data1" label="应退件数：">
										<el-input clearable class="input_right"  @input="addForm.backNeedCount=addForm.backNeedCount.replace(/[^\d]/g,'')" v-model="addForm.backNeedCount"
												  :disabled="type != 'deal'"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="8" :offset="8">
									<el-form-item class="formList" prop="data5" label="确认人：">
										<el-input clearable class="input_right" v-model="addForm.sureUser+' '+addForm.sureTime"
												  disabled></el-input>
									</el-form-item>
								</el-col>
							</el-row>
							<el-row :gutter="40" v-show="addForm.orderBackStatus > '1'">
								<el-col :span="8">
									<el-form-item class="formList" prop="data1" label="换货件数：">
										<el-input clearable class="input_right" v-model="addForm.huanhuoCount"
												  disabled></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item class="formList" prop="data5" label="退款件数：">
										<el-input @change="changeBackCount" clearable class="input_right" @input="addForm.backOunt=addForm.backOunt.replace(/[^\d]/g,'')"  v-model="addForm.backOunt"
												  :disabled="type != 'sure'"></el-input>
									</el-form-item>
								</el-col>
								<el-col :span="8">
									<el-form-item class="formList" prop="data5" label="完成人：">
										<el-input clearable class="input_right" v-model="addForm.completeUser+' '+addForm.completeTime"
												  disabled></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>
					</div>
				</div>
			</el-form>
		</div>
	</div>
</template>
<script>
	import {formatDate} from "@/plugins/formatDate";
	import {getStore} from "@/plugins/localStore";

	export default {
		props: ['type', 'editData'],
		data() {
			return {
				queryForm: {customer: '', time: []},
				tableLoading:false,
				addForm: {
					id: '',
					orderId: '',
					customer: '',
					orderBackStatus: '',
					orderBackStatusCn: '',
					orderBackCode: '',
					handleUser: '',
					wuliuCount: '',
					remark: '',
					baoshuUser: '',
					baoshuTime: '',
					sureUser: '',
					sureTime: '',
					disOrderUser: '',
					disOrderTime: '',

					backNeedCount: '',
					backOunt: '',
					completeTime: '',
					completeUser: '',
					huanhuoCount: '',


				},
				rules: {},

				orderList: [],
				orderInfo: {},
				storageList:[],
				count:0,

				user:{},
				nowTime:''
			}
		},
		methods: {
			changeBackCount(){
				if(!this.addForm.backOunt){
					this.addForm.huanhuoCount = '';
				}else {
					let backCount = parseInt(this.addForm.backOunt);
					let needCount = parseInt(this.addForm.backNeedCount);
					let huanhuoCount = needCount - backCount;
					if(huanhuoCount < 0){
						this.$message.error('换货件数小于0!');
					}
					this.addForm.huanhuoCount = huanhuoCount+'';
				}
			},
			changeBox(item){
				for(let data of this.orderList){
					if(data.id == item.id){
						continue;
					}
					data.checked = false;
				}
			},

			updateOrderBackSure(){
				let self = this;
				if(self.count != 0){
					return;
				}
				if(!self.addForm.backNeedCount){
					self.$message.warning('请填写应退数量');
					return;
				}
				if(parseInt(self.addForm.backNeedCount) < 1){
					self.$message.warning('应退数量大于0');
					return;
				}
				let param = {id:self.addForm.id,sureUser:getStore().userName,backNeedCount:self.addForm.backNeedCount};
				self.$http({
					url: "/jinl/orderBack/updateOrderBackSure",
					method: "post",
					params: param
				}).then(resp => {
					self.count = 0;
					if (resp.success) {
						self.$message.success('确认成功');
						self.$emit('refreshPage');
						return;
					}
					self.$message.error(resp.message);
				});
			},

			updateOrderBackComplete(){
				let self = this;
				if(self.count != 0){
					return;
				}
				if(!self.addForm.backOunt){
					self.$message.warning('请填写退款件数');
					return;
				}
				if(parseInt(self.addForm.backOunt) < 1){
					self.$message.warning('退款件数数量大于0');
					return;
				}
				if(parseInt(self.addForm.huanhuoCount) < 1){
					self.$message.warning('换货件数小于0');
					return;
				}
				let param = {id:self.addForm.id,completeUser:getStore().userName,backOunt:self.addForm.backOunt};
				self.$http({
					url: "/jinl/orderBack/updateOrderBackComplete",
					method: "post",
					params: param
				}).then(resp => {
					self.count = 0;
					if (resp.success) {
						self.$message.success('确认完成');
						self.$emit('refreshPage');
						return;
					}
					self.$message.error(resp.message);
				});
			},

			setOrderBack(){
				let self = this;
				let index = self.orderList.findIndex((item)=>item.checked);
				if(index < 0){
					self.$message.warning('请选择退货订单');
					return;
				}
				let param = {orderId:self.orderList[index].id,handleUser:getStore().userName};
				if(self.count != 0){
					return;
				}
				self.$http({
					url: "/jinl/orderBack/addOrderBackInfo",
					method: "post",
					params:param
				}).then(resp => {
					self.count = 0;
					if (resp.success) {
						self.$message.success('退货成功');
						self.$emit('refreshPage');
						return;
					}
					self.$message.error(resp.message);
				});
			},

			getOrderInfoByParamById(){
				let self = this;
				self.orderInfo = {};
				self.$http({
					url: "/jinl/orderOut/getOrderInfoByParam",
					method: "post",
					params: {id:self.addForm.orderId}
				}).then(resp => {
					if (resp.success) {
						self.orderInfo = resp.result.length> 0?resp.result[0]:{};
						return;
					}
					self.$message.error(resp.message);
				});
			},

			getOrderBackStorageById(){
				let self = this;
				self.storageList = [];
				self.$http({
					url: "/jinl/orderBackStorage/getOrderBackStorageById",
					method: "post",
					params: {id:self.addForm.id}
				}).then(resp => {
					if (resp.success) {
						self.storageList = resp.result||[];
						return;
					}
					self.$message.error(resp.message);
				});
			},

			getOrderInfoByParam() {
				let self = this;
				let param = {};
				param.customer = self.queryForm.customer;
				if (this.queryForm.time && this.queryForm.time.length == 2) {
					//date类型
					param.startTime = formatDate(new Date(self.queryForm.time[0]), 'yyyy-MM-dd')
					param.endTime = formatDate(new Date(self.queryForm.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
				}
				self.orderList = [];
				self.tableLoading = true;
				self.$http({
					url: "/jinl/orderOut/getOrderInfoByParam",
					method: "post",
					params: param
				}).then(resp => {
					self.tableLoading = false;
					if (resp.success) {
						self.orderList = resp.result||[];
						for(let data of self.orderList){
							self.$set(data,'checked',false);
						}
						console.log('===============getOrderInfoByParam==========')
						console.log(self.orderList)
						return;
					}
					self.$message.error(resp.message);
				});
			},

			searchReset() {
				this.queryForm = {customer: '', time: []};
				this.orderList = [];
			},
			submitForm(val) {
				//退货
				if(this.type == 'add'){
					this.setOrderBack();
				}else if(this.type == 'deal'){
					this.updateOrderBackSure();
				}else if(this.type == 'sure'){
					this.updateOrderBackComplete();
				}
			},
		},

		watch: {
			editData(val) {
				console.log('======editData=========')
				console.log(val)
				this.nowTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
				this.user = getStore();
				val.baoshuUser = val.baoshuUser||'';
				val.baoshuTime = val.baoshuTime||'';
				val.sureUser = val.sureUser||'';
				val.sureTime = val.sureTime||'';
				val.disOrderUser = val.disOrderUser||'';
				val.disOrderTime = val.disOrderTime||'';
				val.completeUser = val.completeUser||'';
				val.completeTime = val.completeTime||'';
				val.huanhuoCount = '';
				this.addForm = Object.assign({}, val);
				if(this.type != 'add'){
					this.getOrderInfoByParamById();
				}
				if(val.orderBackStatus > '2'){
					this.getOrderBackStorageById();
				}
				if(this.type == 'deal'){
					this.addForm.sureUser = this.user.userName;
					this.addForm.sureTime = this.nowTime;
				} else if(this.type == 'sure'){
					this.addForm.completeUser = this.user.userName;
					this.addForm.completeTime = this.nowTime;
				}
				if(this.addForm.orderBackStatus == '4'){
					let backCount = parseInt(val.backOunt);
					let needCount = parseInt(val.backNeedCount);
					let huanhuoCount = needCount - backCount;
					this.addForm.huanhuoCount = huanhuoCount+'';
				}

			},
		},
		mounted() {

		},
		components: {}
	}
</script>
<style lang="scss" scoped>
	@import "@/style/dialog.scss";

	.business-workAdd.addProject {
		.nav_list {
			margin-top: 20px;

			._title {
				line-height: 40px;
				color: #00D1D0;

				i {
					font-size: 16px;
					margin-right: 5px;
				}
			}
		}

		.formList {
			margin-bottom: 0px;
			height: 32px;
			display: flex;
			align-items: center;
		}

		.tableList {
			table {
				table-layout: auto;

				tr {
					background: #fff !important;

					td {
						padding: 1px 3px;
					}

					&.noData {
						color: #999;
						height: 40px;
					}

					._span {
						width: 16.666%;
						float: right;
						color: #1890ff;
						cursor: pointer;

						&:hover {
							opacity: .7;
						}
					}
				}

				.edit {
					color: #1890ff;
					padding: 0 5px;
					cursor: pointer;

					&.icons {
						font-size: 16px;
					}

					&:hover {
						opacity: .7;
					}
				}
			}
		}
	}
</style>